Imports Aricie.DNN.Modules.LuceneSearch.Business
Imports DotNetNuke.Services.Localization
Imports Lucene.Net.Index
Imports Lucene.Net.Search

Namespace Aricie.DNN.Modules.LuceneSearch.UI
    Partial Public Class LanguageFilter
        Inherits LuceneFilterControlBase

        Public Sub New()
            QueryMode = UI.QueryMode.Filter
        End Sub

        Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
            If Not IsPostBack Then
                BindLayout()
            End If
        End Sub

        Public Overrides Property Enabled() As Boolean
            Get
                Return Me.ddlLanguages.Enabled
            End Get
            Set(ByVal value As Boolean)
                Me.ddlLanguages.Enabled = value
            End Set
        End Property

        Public Overrides Function GetFilter() As LuceneFilterQuery
            Dim toReturn As New LuceneFilterQuery
            Dim languageFilter As PhraseQuery = Nothing
            Dim languageValue As String = ddlLanguages.SelectedValue
            If languageValue <> "anyAny" Then
                'languageValue = BusinessController.FormatSearchCriteria(languageValue)
                languageFilter = New PhraseQuery()
                languageFilter.Add(New Term(LuceneGeneralField.Language, languageValue))
            End If
            toReturn.FilterQuery = languageFilter
            Return toReturn
        End Function

        Public Overrides Function HasFilter() As Boolean
            Return ddlLanguages.SelectedValue <> "anyAny"
        End Function


        Public Overrides Function GetFieldNames() As IEnumerable(Of String)
            Return New String() {LuceneGeneralField.Language}
        End Function

        Public Overrides Function ParseFilter(ByVal objFilter As LuceneFilterQuery) As Boolean
            If TypeOf objFilter.FilterQuery Is TermQuery Then
                Dim objTerm As Term = DirectCast(objFilter.FilterQuery, TermQuery).Term()
                If objTerm.Field = LuceneGeneralField.Language Then
                    Dim languageValue As String = objTerm.Text
                    Me.ddlLanguages.SelectedValue = languageValue
                    Return True
                End If
            End If
            Return False
        End Function


#Region "Private methods"

        Private Sub BindLayout()

            Localization.LoadCultureDropDownList(Me.ddlLanguages, CultureDropDownTypes.DisplayName, "")
            Me.ddlLanguages.Items.Insert(0, _
                                          New ListItem(Localization.GetString("Language.Any", Me.LocalResourceFile), _
                                                        "anyAny"))
            Me.ddlLanguages.SelectedIndex = 0

        End Sub

#End Region
    End Class
End Namespace
